package 动态规划;

import java.util.Arrays;

/**
 * @author 帅小伙
 * @date 2021/12/27
 * @description
 */
public class Demo05JZ85连续子数组的最大和 {

    public int[] FindGreatestSumOfSubArray(int[] array) {
        int[] dp = new int[array.length];
        dp[0] = array[0];
        int left = 0, right = 0, skipLeft = 0, skipRight = 0, maxlength = 1, maxSum = array[0];
        for (int i = 1; i < array.length; ++i) {
            right++;
            dp[i] = Math.max(dp[i-1]+array[i], array[i]);
            if(dp[i-1]+array[i] < array[i]) left = right;
            if(dp[i] > maxSum || dp[i]==maxSum && (right - left + 1) > maxlength){
                maxlength = right - left + 1;
                skipLeft = left;
                skipLeft = right;
                maxSum = dp[i];
            }
        }
        return Arrays.copyOfRange(array, skipLeft,skipRight+1);
    }
}
